﻿<!DOCTYPE HTML>
<html xmlns:th="http://www.w3.org/1999/xhtml" xmlns:if="http://www.w3.org/1999/xhtml">
<!-- 插入公共模板 _meta -->
<head th:insert="../templates/admin/common/_meta :: common_meta"></head>
<body>

<!-- 插入公共模板 _header -->
<div th:replace="../templates/admin/common/_header :: common_header"></div>

<!-- 插入公共模板 _menu -->
<div th:replace="../templates/admin/common/_menu :: common_menu"></div>

<section class="Hui-article-box">
	<nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 商品管理 <span class="c-gray en">&gt;</span> 商品列表<a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新" ><i class="Hui-iconfont">&#xe68f;</i></a></nav>
	<div class="Hui-article">
		<article class="cl pd-20">
			<div class="cl pd-5 bg-1 bk-gray mt-20">
				<span class="l">
					<a href="javascript:;" onclick="goods_stopBatch()" class="btn btn-danger radius"><i class="Hui-iconfont">&#xe6e2;</i> 批量下架</a>
					<a href="javascript:;" onclick="goods_add('添加商品','goGoodsAdd','','650')" class="btn btn-primary radius"><i class="Hui-iconfont">&#xe600;</i> 添加商品</a>
				</span>
				<span class="r">共有数据：<strong th:text="${goodsVOList.size()}"></strong> 条</span>
			</div>
			<div class="mt-20">
				<table class="table table-border table-bordered table-hover table-bg table-sort">
					<thead>
						<tr class="text-c">
							<th width="25"><input type="checkbox" ></th>
							<th width="40">ID</th>
							<th width="80">商品名称</th>
							<th width="80">所属种类</th>
							<th width="100">商品图片</th>
							<th width="60">商品单价(/元)</th>
							<th width="50">商品数量</th>
							<th width="120">简要描述</th>
							<th width="90">创建者</th>
							<th width="100">创建时间</th>
							<th width="100">更新时间</th>
							<th width="50">状态</th>
							<th width="100">操作</th>
						</tr>
					</thead>
					<tbody>
						<tr class="text-c" th:each="goodsVO,var : ${goodsVOList}">
							<td><input type="checkbox" th:value="${goodsVO.goodsId}" name="goodsId"></td>
							<td th:text="${goodsVO.goodsId}"></td>
							<td th:text="${goodsVO.goodsName}"></td>
							<td th:text="${goodsVO.categoryName}"></td>
							<td>
								<img style="width: 160px;height: 180px;" th:src="|@{'' + ${goodsVO.image} + ''}|">
							</td>
							<td th:text="${#numbers.formatDecimal(goodsVO.price/100.0,1,2)}"> 元</td>
							<td th:text="${goodsVO.quantity}"></td>
							<td th:text="${goodsVO.simpleDescribe}"></td>
							<td th:text="${goodsVO.adminName}"></td>
							<td th:with="createTime=${goodsVO.createTime}" th:text="${#dates.format(createTime, 'yyyy-MM-dd HH:mm:ss')}"></td>
							<td th:with="updateTime=${goodsVO.updateTime}" th:text="${#dates.format(updateTime, 'yyyy-MM-dd HH:mm:ss')}"></td>
							<td class="td-status">
								<span class="label label-success radius" th:if="${goodsVO.isMarketable == 1}">已上架</span>
								<span class="label label-defaunt radius" th:if="${goodsVO.isMarketable == 0}">未上架</span>
							</td>
							<td class="td-manage">
								<a th:if="${goodsVO.isMarketable == 1}" style="text-decoration:none" th:onClick="|goods_stop(this,${goodsVO.goodsId},'stop')|" href="javascript:;" title="下架"><i class="Hui-iconfont">&#xe631;</i></a>
								<a th:if="${goodsVO.isMarketable == 0}" style="text-decoration:none" th:onClick="|goods_start(this,${goodsVO.goodsId},'start')|" href="javascript:;" title="上架"><i class="Hui-iconfont">&#xe6e1;</i></a>
								<a style="text-decoration:none" class="ml-5" th:onclick="|goods_edit('编辑','goGoodsEdit',${goodsVO.goodsId},'','650')|" title="编辑" href="javascript:;" ><i class="Hui-iconfont">&#xe6df;</i></a>
								<a title="删除" href="javascript:;" th:onclick="|goods_del(this,${goodsVO.goodsId})|" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6e2;</i></a>
							</td>
						</tr>
					</tbody>
				</table>
			</div>
		</article>
	</div>
</section>

<!-- 插入公共模版 _footer -->
<div th:replace="../templates/admin/common/_footer :: common_footer"></div>

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="../../lib/My97DatePicker/4.8/WdatePicker.js"></script>
<script type="text/javascript" src="../../lib/datatables/1.10.0/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="../../lib/laypage/1.2/laypage.js"></script>
<script type="text/javascript" th:inline="none">

	/*商品-添加*/
	function goods_add(title,url,w,h){
		layer_show(title,url,w,h);
	}

	/*商品-删除*/
	function goods_del(obj,goodsId){
		layer.confirm('确认要删除吗？',function(index){
			$.ajax({
				type: "GET",
				dateType: "json",
				url: "doGoodsDelete"+"?goodsId="+goodsId,
				success: function (result) {
					if(result.flag = "true") {
						$(obj).parents("tr").remove();
						layer.msg('已删除!',{icon:1,time:1000});
					} else {
						layer.msg('删除失败!',{icon: 2,time:1000});
					}
				},
				error: function (err) {
					layer.msg('请求异常!',{icon: 2,time:1000})
				}
			});

		});
	}

	/*商品-批量下架*/
	function goods_stopBatch() {
		var obj = document.getElementsByName("goodsId");
		var goodsIds = [];
		for(i in obj) {
			if(obj[i].checked){
				goodsIds.push(obj[i].value);
			}
		}
		console.log(goodsIds);
		layer.confirm('确认要下架吗？',function(index){
			$.ajax({
				type: "GET",
				dateType: "json",
				url: "doStopBatchGoods"+"?goodsIds="+goodsIds,
				success: function (result) {
					if(result.flag = "true") {
						layer.msg('下架成功,请刷新!',{icon: 6,time:1000});
					} else {
						layer.msg('失败,含有'+result.number+'个下架失败!',{icon: 2,time:1000});
					}
				},
				error: function (err) {
					layer.msg('请求异常!',{icon: 2,time:1000})
				}
			});
		});
	}

	/*商品-下架*/
	function goods_stop(obj,goodsId,state){
		layer.confirm('确认要下架吗？',function(index){
			$.ajax({
				type: "GET",
				dateType: "json",
				url: "doGoodsIsMarketable"+"?goodsId="+goodsId + "&state=" + state,
				success: function (result) {
					if(result.flag = "true") {
						$(obj).parents("tr").find(".td-manage").prepend('<a style="text-decoration:none" onClick="goods_start(this,'+goodsId+',\'start\')" href="javascript:;" title="上架"><i class="Hui-iconfont">&#xe6e1;</i></a>');
						$(obj).parents("tr").find(".td-status").html('<span class="label label-defaunt radius">已停用</span>');
						$(obj).remove();
						layer.msg('已下架!',{icon: 5,time:1000});
					} else {
						layer.msg('下架失败!',{icon: 2,time:1000});
					}
				},
				error: function (err) {
					layer.msg('请求异常!',{icon: 2,time:1000})
				}
			});
		});
	}

	/*商品-上架*/
	function goods_start(obj,goodsId,state){
		layer.confirm('确认要上架吗？',function(index){
			$.ajax({
				type: "GET",
				dateType: "json",
				url: "doGoodsIsMarketable" + "?goodsId=" + goodsId + "&state=" + state,
				success: function (result) {
					if(result.flag = "true") {
						$(obj).parents("tr").find(".td-manage").prepend('<a style="text-decoration:none" onClick="goods_stop(this,'+goodsId+',\'stop\')" href="javascript:;" title="下架"><i class="Hui-iconfont">&#xe631;</i></a>');
						$(obj).parents("tr").find(".td-status").html('<span class="label label-success radius">已上架</span>');
						$(obj).remove();
						layer.msg('已上架!',{icon: 6,time:1000});
					} else {
						layer.msg('上架失败!',{icon: 2,time:1000});
					}
				},
				error: function (err) {
					layer.msg('请求异常!',{icon: 2,time:1000})
				}
			});
		});
	}

	/*商品-编辑*/
	function goods_edit(title,url,goodsId,w,h){
		url = url + "?goodsId=" + goodsId;
		layer_show(title,url,w,h);
	}


	/* 表格配置 */
	$(function(){
		$('.table-sort').dataTable({
			"aaSorting": [[ 1, "asc" ]],//默认第几个排序
			"bStateSave": true,//状态保存
			"aoColumnDefs": [
			  //{"bVisible": false, "aTargets": [ 3 ]} //控制列的隐藏显示
			  {"orderable":false,"aTargets":[0,4,12]}// 制定列不参与排序
			]
		});


	});

</script>
<!--/请在上方写此页面业务相关的脚本-->
</body>
</html>